import type { EventBusFunction, dayjsFunction, debounceFunction, mittEvent } from '@/types'
import dayJs from 'dayjs'
import mitt from 'mitt'

/**
 * 时间格式函数
 * @returns
 */
export const dayjs: dayjsFunction = dayJs

/**
 * 事件总线
 */
export const EventBus: EventBusFunction = mitt<mittEvent>()

/**
 * 防抖
 * @param func 回调函数
 * @param wait 防抖时间
 * @returns 方法
 */
let timeout: number
export const debounce: debounceFunction = (func: Function, wait: number = 1000) => {
  function _debounce() {
    timeout && clearTimeout(timeout)
    timeout = setTimeout(() => {
      func && typeof func === 'function' && func()
    }, wait)
  }
  _debounce()
}
